// packageUser/pages/evaluate/quality-evaluate.js
import OrderService from '../../../service/OrderService';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    factoryId: '',
    score: 0,
    content: '',
    fileList:[],
    evaluate:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.orderService = new OrderService();
    const { id, factoryId} = options;
    this.setFieldValue("orderId", id);
    this.setData({factoryId})   
  },

  setFieldValue: function (field, value) {
    const { evaluate } = this.data;
    evaluate[field] = value;
    this.setData({ evaluate });
  },
  onScoreChage: function(e){
    this.setData({score: e.detail.score})
  },
  onImagesChange: function(e){
    const images = e.detail.images;
    const fileList = images.map(item=>{
      return {fileUrl: item};
    })
    this.setFieldValue("fileList", fileList);
  },
  onContentChange: function(e){
    this.setFieldValue("content", e.detail.value);
  },
  validForm: function (form) {
    if (!form.orderId) {
      this.setData({ errMsg: '订单ID为空' })
      return false;
    }
    if (!form.content) {
      this.setData({ errMsg: '评价内容不能为空' })
      return false;
    }
    this.setData({ errMsg: '' })
    return true;
  },
  submit: function(e){
    const { factoryId, score, evaluate} = this.data;
    if (!factoryId) {
      this.setData({ errMsg: '工厂方ID为空' })
      return;
    }
    if (!this.validForm(evaluate)) {
      return;
    }
    // console.log(this);
    this.orderService.qualityEval({ factoryId, score, evaluate}).then(res=>{
      if(res.status == 0){
        wx.navigateBack()
      }
    })
  }
})